const KerwinToken = artifacts.require('KerwinToken.sol')
const exchange = artifacts.require('Exchange.sol')
const ETHER_ADDRESS="0x0000000000000000000000000000000000000000"
const fromWei =(bn)=>{
    return web3.utils.fromWei(bn, 'ether');
}
const toWei = (number)=>{
    return web3.utils.toWei(number.toString(), 'ether');
}
const wait =(seconds)=>{
    const milliseconds = seconds * 1000
    return new Promise(resolve=>setTimeout(resolve,milliseconds))
}
module.exports = async function(callback) {
    const Kerwintoken = await KerwinToken.deployed()
    const Exchanges = await exchange.deployed()
    const accounts = await web3.eth.getAccounts()//获取所有账号，模拟的10个账号

    //第一步account0->account1 10w KWT币
    await Kerwintoken.transfer(accounts[1], toWei(100000),{
        from: accounts[0]
    })

    //第二步account0->交易所：存入 100以太币
    //必须先授权
    await Kerwintoken.approve(exchange.address, toWei(100000),{from:accounts[0]})
    await Exchanges.depositEther({
        from:accounts[0],
        value:toWei(100)
    })
    let res1 = await Exchanges.tokens(ETHER_ADDRESS,accounts[0])
    console.log("account[0]-在交易所的以太币",fromWei(res1))

    //第二步account0->交易所存入100000KWT
    //交易所转账
    await Exchanges.depositToken(Kerwintoken.address,toWei(100000),{from:accounts[0]})
    let res2 = await Exchanges.tokens(Kerwintoken.address,accounts[0])
    console.log("account[0]-在交易所的KWT币：",fromWei(res2))

    //第三步account1->交易所存入50以太币
    //必须先授权
    await Kerwintoken.approve(exchange.address, toWei(100000),{from:accounts[1]})
    await Exchanges.depositEther({
        from:accounts[1],
        value:toWei(50)
    })
    let res4 = await Exchanges.tokens(ETHER_ADDRESS,accounts[1])
    console.log("account[1]-在交易所的以太币",fromWei(res4))
    //第三步account1->交易所存入5000KWT

    //交易所转账
    await Exchanges.depositToken(Kerwintoken.address,toWei(50000),{from:accounts[1]})
    let res3 = await Exchanges.tokens(Kerwintoken.address,accounts[1])
    console.log("account[1]-在交易所的KWT币：",fromWei(res3))

    let orderId = 0;
    let res;
    //创建订单
    res = await Exchanges.makeOrder(Kerwintoken.address,toWei(1000),ETHER_ADDRESS,toWei(0.1),{from:accounts[0]});
    orderId = res.logs[0].args.id
    console.log("创建了一个订单：id=", orderId)
    await wait(1)
    //取消订单
    res = await Exchanges.makeOrder(Kerwintoken.address,toWei(2000),ETHER_ADDRESS,toWei(0.2),{from:accounts[0]});
    orderId = res.logs[0].args.id
    await  Exchanges.cancelOrder(orderId,{from:accounts[0]})
    console.log("取消一个订单:id=",orderId)
    await wait(1)
    //完成订单
    res = await Exchanges.makeOrder(Kerwintoken.address,toWei(3000),ETHER_ADDRESS,toWei(0.3),{from:accounts[0]});
    orderId = res.logs[0].args.id
    await Exchanges.fillOrder(orderId,{from:accounts[1]})
    console.log("完成一个订单:id=",orderId)

    console.log("account[0]-在交易所的以太币",fromWei(await Exchanges.tokens(ETHER_ADDRESS,accounts[0])))
    console.log("account[0]-在交易所的KWT",fromWei(await Exchanges.tokens(Kerwintoken.address,accounts[0])))

    console.log("account[1]-在交易所的以太币",fromWei(await Exchanges.tokens(ETHER_ADDRESS,accounts[1])))
    console.log("account[1]-在交易所的KWT",fromWei(await Exchanges.tokens(Kerwintoken.address,accounts[1])))

    console.log("account[2]-在交易所的KWT",fromWei(await Exchanges.tokens(Kerwintoken.address,accounts[2])))
    callback()
}